﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Iit.ITBI.MDSManager
{
    class CustomEntitiy : CustomObject
    {

        
        public CustomModel Model
        {
            get;
            set;
        }

        public Collection<CustomAttribute> Attributes
        {
            get;
            set;
        }

        public string EntityTableName
        {
            get;
            set;
        }

        public string StagingBase
        {
            get;
            set;
        }

        public CustomEntitiy(string Name, Guid MUID) :base(Name,MUID) { }

        public CustomEntitiy(string Name, Guid MUID, string ModelName, Guid ModelMUID)
        {
            this.Name = Name;
            this.MUID = MUID;
            this.IsSystem = false;
            this.Model = new CustomModel(ModelName, ModelMUID);
        }

        public CustomEntitiy(DataRow MasterToSlaveMappingData, SlaveOrMaster EntityType, MDSQueryVersion QueryVersion)
        {
            if (QueryVersion == MDSQueryVersion.GetMasterToSlaveMappingV1)
            {

                this.Model = new CustomModel(MasterToSlaveMappingData, EntityType, QueryVersion);
                
                //depending on the entity type (master or slave) we need to refer to different columns names in the datarow
                switch (EntityType)
                {
                    case SlaveOrMaster.Master:

                        base.Name = MasterToSlaveMappingData["MasterEntity_Name"].ToString();

                        base.MUID = (Guid)MasterToSlaveMappingData["MasterEntity_MUID"];

                        base.Exists = true;

                        this.EntityTableName = MasterToSlaveMappingData["MasterEntityTable"].ToString();

                        this.StagingBase = MasterToSlaveMappingData["MasterEntityStagingBase"].ToString();

                        break;

                    case SlaveOrMaster.Slave:

                        base.Name = MasterToSlaveMappingData["SlaveEntity_Name"].ToString();

                        ///if the Entity MUID is null, then the Entity does not exists in the slave model
                        ///therefore we update the Exists property accordantly, and we create masterAttributeMetadata new guid for later use
                        if (MasterToSlaveMappingData["SlaveEntity_MUID"].ToString().Length != 0)
                        {
                            base.MUID = (Guid)MasterToSlaveMappingData["SlaveEntity_MUID"];

                            this.EntityTableName = MasterToSlaveMappingData["SlaveEntityTable"].ToString();

                            this.StagingBase = MasterToSlaveMappingData["SlaveEntityStagingBase"].ToString();

                            base.Exists = true;
                        }
                        else
                        {
                            base.MUID = new Guid();

                            base.Exists = false;
                        }

                        break;
                }

                base.Initialize();
            }
            else
            {
                StringBuilder msg = new StringBuilder();
                msg.AppendFormat("Query Version {0} is not supported.", QueryVersion.ToString());
                throw new NotSupportedException(msg.ToString());
            }
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();

            str.AppendLine(Model.ToString());
            str.AppendLine("Entity: " + base.ToString());

            return str.ToString();
        }
        
    }
}
